package com.lims;

import cn.hutool.core.util.ReflectUtil;
import com.lims.vo.A;
import com.lims.vo.B;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author limaosheng
 * @create 2024-04-01 11:45
 * @desc
 */
public class 实体对象赋值代码生成 {
    /**
     * 不同对象的赋值代码，支持字段相同，但是类型不同的属性赋值
     * @param args
     */
    public static void main(String[] args) {
        //数据来源对象
        A source = new A();
        Field[] form = ReflectUtil.getFields(source.getClass());
        //目标对象
        B target = new B();
        Field[] to = ReflectUtil.getFields(target.getClass());
        //指定实体变量名，不指定则类首字母变小写
        String fromEntityName = "";
        String toEntityName = "";
        if(fromEntityName.equals("")){
            fromEntityName = toLowerCaseFirst(source.getClass().getSimpleName());
        }
        if(toEntityName.equals("")){
            toEntityName = toLowerCaseFirst(target.getClass().getSimpleName());
        }
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        for(Field toField : to){
            String name = toField.getName();
            String typeName = toField.getType().getTypeName();
            //过滤ID字段
            if(name.equals("id") || name.equals("index")){
                continue;
            }
            //标记
            Boolean has = false;
            for(Field fromField : form){
                //找到一样的字段
                if(fromField.getName().equals(name)){
                    has = true;
                    //判断类型是否一致
                    if(fromField.getType().getTypeName().equals(typeName)){
                        System.out.println(toEntityName + ".set" + toUpperCaseFirst(name) + "(" + fromEntityName + ".get" + toUpperCaseFirst(name) + "());");
                    }else {
                        //LocalDate->LocalDateTime
                        if(typeName.equals(LocalDateTime.class.getTypeName()) && fromField.getType().getTypeName().equals(LocalDate.class.getTypeName())){
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toLDT("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else if(typeName.equals(Integer.class.getTypeName()) && fromField.getType().getTypeName().equals(Long.class.getTypeName())){
                            //Long -> Integer
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toI("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else if(typeName.equals(Long.class.getTypeName()) && fromField.getType().getTypeName().equals(Integer.class.getTypeName())){
                            //Integer -> Long
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toL("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else if(typeName.equals(String.class.getTypeName()) && fromField.getType().getTypeName().equals(Integer.class.getTypeName())){
                            //Integer -> String
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toS("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else if(typeName.equals(String.class.getTypeName()) && fromField.getType().getTypeName().equals(Long.class.getTypeName())){
                            //Long -> String
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toS("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else if(typeName.equals(Long.class.getTypeName()) && fromField.getType().getTypeName().equals(String.class.getTypeName())){
                            //String -> Long
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toL("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else if(typeName.equals(Integer.class.getTypeName()) && fromField.getType().getTypeName().equals(String.class.getTypeName())){
                            //String -> Integer
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toI("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else if(typeName.equals(LocalDateTime.class.getTypeName()) && fromField.getType().getTypeName().equals(Long.class.getTypeName())){
                            //Long -> LocalDateTime
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toLDT("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else if(typeName.equals(Long.class.getTypeName()) && fromField.getType().getTypeName().equals(LocalDateTime.class.getTypeName())){
                            //LocalDateTime -> Long
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toL("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else if(typeName.equals(Float.class.getTypeName()) && fromField.getType().getTypeName().equals(Double.class.getTypeName())){
                            //Double -> Float
                            System.out.println(toEntityName + ".set"+toUpperCaseFirst(name)+"(EsSet.toF("+fromEntityName+".get"+toUpperCaseFirst(name)+"()));");
                        }else {
                            sb2.append(toEntityName + ".set" + toUpperCaseFirst(name) + "(???);//" + fromField.getType().getTypeName() + "转" + typeName + "\n");
                        }
                    }
                }
            }
            if(!has){
               sb1.append(toEntityName + ".set" + toUpperCaseFirst(name) + "(?);\n");
            }
        }
        System.out.println(sb2);
        System.out.println(sb1);

    }
    public static String toLowerCaseFirst(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static String toUpperCaseFirst(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
}
